new Vue({
    el: '#app',
    data() {
        return {
            form: {
                App_ID: '',
                Client_ID: '',
                Client_Secret: '',
                Robot_Code: '',
                Application_Name: '',
                Enterprise_Info_SerialNumber: $.cookie('token'),
            },
            isEdit: false,
            message_form: {
                page: 1,
                limit: 10,
                App_ID: sessionStorage.getItem("App_ID"),
                Enterprise_Info_SerialNumber: $.cookie('enterprise'),
                User_Name: ''
            },
            message_total: 0,
            message_tableData: [],
            rules: {
                App_ID: [{ required: true, message: '请输入App_ID', trigger: 'blur' }],
                Client_ID: [{ required: true, message: '请输入功能ID', trigger: 'blur' }],
                Client_Secret: [{ required: true, message: '请输入密钥', trigger: 'blur' }],
                Application_Name: [{ required: true, message: '请输入功能名称', trigger: 'blur' }],
            },
            loading: false,
        }
    },
    mounted() {
        const App_ID = sessionStorage.getItem("App_ID")
        this.getData(App_ID)
        this.getMessageList(this.message_form)
    },
    methods: {
        getData(Primary_Key) {//查询配置信息表
            const that = this;
            $.ajax({
                url: `${DomainName}/getInquiryIDOriginal?token=${$.cookie('token')}`,
                type: 'POST',
                data: {
                    Data: 'dingtalk_application',
                    Primary_Key,
                    Primary_Key_Name: 'App_ID'
                },
                success: function (res) {
                    if (res.length > 0) {
                        that.form = res[0];
                    }
                },
                error: function () {
                    that.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        handleMessageSizeChange(val) {
            this.message_form.page = 1;
            this.message_form.limit = val;
            this.getList(this.message_form);
        },
        handleMessageCurrentChange(val) {
            this.message_form.page = val;
            this.getList(this.message_form);
        },
        getMessageList(data) {//查询消息推送配置表
            let _this = this
            $.ajax({
                type: 'POST',
                url: `${DomainName}/queryDingTalkMessage?token=${$.cookie('token')}`,
                data,
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                success: function (Result) {
                    if (Result.Status == 'success') {
                        _this.message_tableData = Result.tableData
                        _this.message_total = parseInt(Result.limit)
                    }
                },
                dataType: "json"
            });
        },
        MessageAdd() {//添加消息推送实例
            sessionStorage.setItem('App_ID', this.form.App_ID);
            window.location.href = './Message_Add.html?App_ID=' + this.form.App_ID;
        },
        MessageSerach() {//筛选器
            this.getMessageList(this.message_form)
        },
        MessageEdit(Dingtalk_Message_SerialNumber) {//修改消息推送实例
            sessionStorage.setItem('Dingtalk_Message_SerialNumber', Dingtalk_Message_SerialNumber);
            window.location.href = './Message_Edit.html?Dingtalk_Message_SerialNumber=' + Dingtalk_Message_SerialNumber;
        },
        MessageDelete(Primary_Key) {//删除
            const that = this
            this.$confirm('警告！此操作将删除该消息推送实例, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                $.ajax({
                    url: DomainName + '/DeleteAction?token=' + $.cookie('token'),
                    type: 'POST',
                    data: {
                        TableName: 'dingtalk_message_config',
                        Primary_Key,
                        Primary_Key_Name: 'Dingtalk_Message_SerialNumber',
                        Operation_Name: '删除消息推送实例'
                    },
                    success: function (res) {
                        if (res.Status == 'success') {
                            that.getMessageList(that.message_form)
                            that.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }
                    },
                    complete: function (XMLHttpRequest) {
                        if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                        }
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'warning',
                    message: '已取消操作'
                });
            });
        },
        changeIsEdit(is, formName) {//修改信息开关校验
            const that = this;
            this.$refs[formName].validate((valid) => {
                if (!valid && !is) {
                    that.isEdit = !is
                    return false
                }
            })
        },
        Send(Dingtalk_Message_SerialNumber) {//修改信息开关校验
            const that = this;
            this.$confirm('是否向"推送人员"中成员发送该消息模板?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                $.ajax({
                    url: `${DomainName}/callBatchSendAction?token=${$.cookie('token')}`,
                    type: 'POST',
                    data: {
                        Dingtalk_Message_SerialNumber
                    },
                    success: function (res) {
                        that.$message.success("发送成功")
                        console.log(res);
                    },
                    error: function () {
                        that.$message.error("操作失败，请稍后再试")
                    },
                    complete: function (XMLHttpRequest) {
                        that.loading = false
                        if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                        }
                    }
                })
            }).catch(() => { });
        },
        submitForm() {
            const that = this
            that.loading = true
            const { add, modify } = that.deal()
            let data = {}
            if (add == null) {
                data = {
                    Data: JSON.stringify({ Modify: { dingtalk_application: [modify] } }),
                    Operation_Name: '更新钉钉应用'
                }
            } else {
                data = {
                    Data: JSON.stringify({ Modify: { dingtalk_application: [modify] }, Add: { dingtalk_application: [add] } }),
                    Operation_Name: '配置钉钉应用'
                }
            }
            $.ajax({
                url: `${DomainName}/getEditAction?token=${$.cookie('token')}`,
                type: 'POST',
                data,
                success: function (res) {
                    if (res.Status === 'success') {
                        that.$message.success("保存成功")
                        that.getData(App_ID)
                    } else {
                        that.$message.error("提交失败，请稍后再试")
                    }
                },
                error: function () {
                    that.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        deal() {
            const modify = deepClone(this.form)
            return { add: null, modify };
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})